<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="public/css.css"> 
<h1>Trang Quản Lý Fans Page</h1>
<?php
require_once './model/db3.php';
$query = sprintf("SELECT * FROM pages LIMIT %u;", PER_PAGE);
$pages = getPages($query);
$companies = getCompanies("SELECT * FROM companies");
$apps = getApps("SELECT * FROM apps");
$apps_pages = getApps_pages("SELECT * FROM  apps_pages");
$keyword = NULL;
$ratingMin = NULL;
$ratingMax = NULL;
  
?>

<form method="POST">
    Từ khóa : <input type="text" name="keyword"><br>
    Công Ty :  <select name='id_company'>
        <?php foreach ($companies as $company): ?>
            <option value="<?php echo $company['id'] ?>"><?php echo $company["name"] ?></option>
        <?php endforeach; ?>
    </select> <br>
    Ứng Dụng :  <select name ='id_app'>
        <?php foreach ($apps as $app): ?>
            <option value="<?php echo $app['id'] ?>"><?php echo $app["name"] ?></option>
        <?php endforeach; ?>
    </select> <br>
    Quốc Gia :  <select>
        <?php foreach ($pages as $page): ?>
            <option><?php echo $page["country"] ?></option>
        <?php endforeach; ?>
    </select> <br>
    Số like nhỏ nhất &nbsp;: <input type="text" name="ratingMin"><br>
    Số like lớn nhất : <input type="text" name="ratingMax"><br>
    <input type="submit" name="submit" value="Tìm Kiếm">
</form>
<?php if (isset($_POST['submit'])): ?>
    <?php $keyword = $_POST['keyword']; ?>
    <?php $ratingMin = (double) $_POST['ratingMin']; ?>
    <?php $ratingMax = (double) $_POST['ratingMax']; ?>
    <?php $apps_pages= (double)$_POST['id_app'];?>
<?php endif; ?>
<table id="table1" border="1px">
    <tr class="khung">
        <td class="pid">STT </td>
        <td class="picture">Hình Ảnh Đại Diện </td>
        <td class="id">Số id </td>
        <td class="name">Tên Fans page </td>
        <td class="likes">Lượt Thích </td>
        <td class="talking">Lượt nhắc đến</td>
        <td class="category">Lĩnh Vực </td>
        <td class="link">Link Fans Page </td>
        <td class="website">Website </td>
        <td class="country">Quốc Gia </td>
    </tr>
    <?php foreach ($pages as $page): ?>
        <?php if (empty($keyword)): ?>
            <?php if (empty($ratingMin) && empty($ratingMax)): ?>
                <tr>
                    <td><?php echo $page["pid"] ?></td>
                    <td><img src ='<?php echo $page["picture"] ?>'</td>
                    <td><?php echo $page["id"] ?></td>
                    <td><?php echo $page["name"] ?></td>
                    <td><?php echo $page["likes"] ?></td>
                    <td><?php echo $page["talking_about_count"] ?></td>
                    <td><?php echo $page["category"] ?></td>
                    <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                    <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                    <td><?php echo $page["country"] ?></td>
                </tr>
            <?php elseif (empty($ratingMax)): ?>
                <?php if ($ratingMin <= (double) $page["likes"]): ?>
                    <tr>
                        <td><?php echo $page["pid"] ?></td>
                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                        <td><?php echo $page["id"] ?></td>
                        <td><?php echo $page["name"] ?></td>
                        <td><?php echo $page["likes"] ?></td>
                        <td><?php echo $page["talking_about_count"] ?></td>
                        <td><?php echo $page["category"] ?></td>
                        <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                        <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                        <td><?php echo $page["country"] ?></td>
                    </tr>
                <?php endif; ?>
            <?php else: ?>
                <?php if (($ratingMin <= (double) $page["likes"]) && ($ratingMax > $page["likes"])): ?>
                    <tr>
                        <td><?php echo $page["pid"] ?></td>
                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                        <td><?php echo $page["id"] ?></td>
                        <td><?php echo $page["name"] ?></td>
                        <td><?php echo $page["likes"] ?></td>
                        <td><?php echo $page["talking_about_count"] ?></td>
                        <td><?php echo $page["category"] ?></td>
                        <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                        <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                        <td><?php echo $page["country"] ?></td>
                    </tr>
                <?php endif; ?>
            <?php endif; ?>
        <?php else: ?>
            <?php if (strstr($page["name"], $keyword)): ?>
                <?php if (empty($ratingMin) && empty($ratingMax)): ?>
                    <tr>
                        <td><?php echo $page["pid"] ?></td>
                        <td><img src ='<?php echo $page["picture"] ?>'</td>
                        <td><?php echo $page["id"] ?></td>
                        <td><?php echo $page["name"] ?></td>
                        <td><?php echo $page["likes"] ?></td>
                        <td><?php echo $page["talking_about_count"] ?></td>
                        <td><?php echo $page["category"] ?></td>
                        <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                        <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                        <td><?php echo $page["country"] ?></td>
                    </tr>
                <?php elseif (empty($ratingMax)): ?>
                    <?php if ($ratingMin <= (double) $page["likes"]): ?>
                        <tr>
                            <td><?php echo $page["pid"] ?></td>
                            <td><img src ='<?php echo $page["picture"] ?>'</td>
                            <td><?php echo $page["id"] ?></td>
                            <td><?php echo $page["name"] ?></td>
                            <td><?php echo $page["likes"] ?></td>
                            <td><?php echo $page["talking_about_count"] ?></td>
                            <td><?php echo $page["category"] ?></td>
                            <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                            <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                            <td><?php echo $page["country"] ?></td>
                        </tr>
                    <?php endif; ?>
                <?php else: ?>
                    <?php if (($ratingMin <= (double) $page["likes"]) && ($ratingMax > $page["likes"])): ?>
                        <tr>
                            <td><?php echo $page["pid"] ?></td>
                            <td><img src ='<?php echo $page["picture"] ?>'</td>
                            <td><?php echo $page["id"] ?></td>
                            <td><?php echo $page["name"] ?></td>
                            <td><?php echo $page["likes"] ?></td>
                            <td><?php echo $page["talking_about_count"] ?></td>
                            <td><?php echo $page["category"] ?></td>
                            <td><a href='<?php echo $page["link"] ?>'><?php echo $page["link"] ?></td>
                            <td><a href='<?php echo $page["website"] ?>'><?php echo $page["website"] ?></td>
                            <td><?php echo $page["country"] ?></td>
                        </tr>
                    <?php endif; ?>
                <?php endif; ?>
            <?php endif; ?> 
        <?php endif; ?> 
    <?php endforeach; ?>
</table>